<?php
/**--------------------
 * Date: 2018/3/05
 * FileName:Grad.php 
 * ProjectName:ZhiShiRongYao
 * -------------------- */


namespace app\api\controller;


use app\api\ApiBase;
use think\Db;

class Grad extends ApiBase
{
    /**
     * 获取排位赛列表
     *
     * @throws \think\exception\DbException 数据库报错
     * @return string
     */
    public function getGradList(){    
        $data=[];
        $data['grad_list']=Db::name('grad')->field('id,grad_logo,name,reward_point,consume_point,pass_stars')->order(['sort'=>'asc'])->select();
        $user_start=Db::name('user')->where(['id'=>$this->user_info['id']])->field('grad_id,grad_name,curr_grad_start')->find();
        $over_level=Db::name('grad')->field('id,grad_logo,name,reward_point,consume_point,pass_stars')->order(['sort'=>'desc'])->limit(1)->find();
        $activity=Db::name('activity')->where(['status'=>1])->field('id,title,start_time,end_time,poster_pic')->order(['id'=>'desc'])->find();
        //---赛季start---//
        $user_extend=Db::name('user_info_extend')->where(['user_id'=> $this->user_info['id']])->find();
        $play=[];
        if($user_extend){
            $now=time();
            if($user_extend['exp_limit_time'] && $user_extend['exp_limit_time']-$now>0){//是否过期
                $QuestionsModel = \think\Loader::model('QuestionsModel');
                $play['exp_limit_time'] = $QuestionsModel->expirationTime($user_extend['exp_limit_time']);
            }else{
                $play['exp_limit_time']=0;
            }
            if($user_extend['point_limit_time'] && $user_extend['point_limit_time']-$now>0){//是否过期
                $QuestionsModel = \think\Loader::model('QuestionsModel');
                $play['point_limit_time'] = $QuestionsModel->expirationTime($user_extend['point_limit_time']);                     
            }else{
                $play['point_limit_time']=0;
            }
            $play['exp_limit_num']=$user_extend['exp_limit_num']?$user_extend['exp_limit_num']:'';
            $play['point_limit_num']=$user_extend['point_limit_num']?$user_extend['point_limit_num']:'';
            $play['jf_limit_num']=$user_extend['jf_limit_num']?$user_extend['jf_limit_num']:'';
        }else{
            $play['exp_limit_time']=0;
            $play['point_limit_time']=0;
        }
        //---赛季end---//
        $activity['start_time']= date("Y.m.d", $activity['start_time']);
        $activity['end_time']= date("Y.m.d", $activity['end_time']);
        foreach($data['grad_list'] as $key=>$value){ //等级星星转为数组
            if($over_level['id']==$value['id']){//最后段位星星数不可超过6
                if($over_level['pass_stars']>6){
                    $value['pass_stars']=$data['grad_list'][$key]['pass_stars']=6;
                }
            }
            for ($x=1; $x<=$value['pass_stars']; $x++) {
                $data['grad_list'][$key]['stars'][$x]=$x;
            }
            if($user_start['grad_id']==$value['id']){
                $user_start['nopass_stars']=$value['pass_stars']-$user_start['curr_grad_start'];//未通过的星星数
            }
        }
        $data['user_level']=$user_start;
        $data['activity']=$activity;
        $data['play']=$play;

        $book_up_rule=\think\Loader::model('UserModel')->knowledge_up($this->user_info['id']);//获取用户升级的知识书和使用道具
        $data['up_rule']=$book_up_rule['up_rule'];
        $data['use_prop']=$book_up_rule['use_prop'];
        if($data){
            return $this->json_success('ok', $data);
        }else{
            return $this->json_error('暂无数据！');
        }
    }

    /**
     * 获取段位列表
     * @return string
     */
    public function getGradDetail(){
        $lists = db("grad")
            ->alias('g')
            ->join('box b','g.box_id = b.id','left')
            ->field("g.id,g.name,g.user_grad_logo,g.reward_box as num,b.name as box_name,b.logo")
            ->order("g.sort desc")
            ->select();
        if($lists){
            return $this->json_return(1, '段位列表获取成功', $lists);
        }else{
            return $this->json_error('暂无数据！');
        }
    }
    
    public function getProp(){
        $map=[];
        $prop_info=[];
        $user_goods=[];
        $map['id']= input('id');
        $map['type']=array('neq',4);
        $map['action_type']=array('in',array(1,2));
        $prop_info=Db::name('prop')->where($map)->find();
        $user_goods=Db::name('user_goods')->where(['target_id'=>$prop_info['id'],'user_id'=> $this->user_info['id']])->find();
        $prop_info['num']=$user_goods['num']?$user_goods['num']:0;
        $prop_info['user_goods_id']=$user_goods['id']?$user_goods['id']:'';
        if($prop_info){
            return $this->json_success('ok',$prop_info);
        }else{
            return $this->json_error('暂无数据！');
        }  
    }

    

}